package org.vaadin.pekka.distributionbartable.widget.client.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.dom.client.Element;

public class DistributionBarTableData {

	private int bars = -1;
	private int colorMode = 0;
	private int sortMode = 0;
	private int sortedBySectionId = 0;
	private int minimumSectionWidth = 30;
	private boolean showEmpty = true;
	private String[] barCaptions;
	private String[] totalCaptions;
	private final List<Element> distributionBars = new ArrayList<Element>(1);
	private List<String[]> sectionCaptions;
	private List<String[]> descriptions;
	private List<int[]> sizes;
	private int total = 0;
	private int sections = 0;

	/**
	 * @return the bars
	 */
	public int getBars() {
		return bars;
	}

	/**
	 * @param bars
	 *            the bars to set
	 */
	public void setBars(int bars) {
		this.bars = bars;
	}

	/**
	 * @return the colorMode
	 */
	public int getColorMode() {
		return colorMode;
	}

	/**
	 * @param colorMode
	 *            the colorMode to set
	 */
	public void setColorMode(int colorMode) {
		this.colorMode = colorMode;
	}

	/**
	 * @return the sortMode
	 */
	public int getSortMode() {
		return sortMode;
	}

	/**
	 * @param sortMode
	 *            the sortMode to set
	 */
	public void setSortMode(int sortMode) {
		this.sortMode = sortMode;
	}

	/**
	 * @return the sortedBySectionId
	 */
	public int getSortedBySectionId() {
		return sortedBySectionId;
	}

	/**
	 * @param sortedBySectionId
	 *            the sortedBySectionId to set
	 */
	public void setSortedBySectionId(int sortedBySectionId) {
		this.sortedBySectionId = sortedBySectionId;
	}

	/**
	 * @return the minimumSectionWidth
	 */
	public int getMinimumSectionWidth() {
		return minimumSectionWidth;
	}

	/**
	 * @param minimumSectionWidth
	 *            the minimumSectionWidth to set
	 */
	public void setMinimumSectionWidth(int minimumSectionWidth) {
		this.minimumSectionWidth = minimumSectionWidth;
	}

	/**
	 * @return the showEmpty
	 */
	public boolean isShowEmpty() {
		return showEmpty;
	}

	/**
	 * @param showEmpty
	 *            the showEmpty to set
	 */
	public void setShowEmpty(boolean showEmpty) {
		this.showEmpty = showEmpty;
	}

	/**
	 * @return the barCaptions
	 */
	public String[] getBarCaptions() {
		return barCaptions;
	}

	/**
	 * @param barCaptions
	 *            the barCaptions to set
	 */
	public void setBarCaptions(String[] barCaptions) {
		this.barCaptions = barCaptions;
	}

	/**
	 * @return the totalCaptions
	 */
	public String[] getTotalCaptions() {
		return totalCaptions;
	}

	/**
	 * @param totalCaptions
	 *            the totalCaptions to set
	 */
	public void setTotalCaptions(String[] totalCaptions) {
		this.totalCaptions = totalCaptions;
	}

	/**
	 * @return the distributionBars
	 */
	public List<Element> getDistributionBars() {
		return distributionBars;
	}

	public void addBar(Element bar) {
		distributionBars.add(bar);
	}

	public boolean removeBar(Element bar) {
		return distributionBars.remove(bar);
	}

	/**
	 * @return the sectionCaptions
	 */
	public List<String[]> getSectionCaptions() {
		return sectionCaptions;
	}

	/**
	 * @param sectionCaptions
	 *            the sectionCaptions to set
	 */
	public void setSectionCaptions(List<String[]> sectionCaptions) {
		this.sectionCaptions = sectionCaptions;
	}

	/**
	 * @return the descriptions
	 */
	public List<String[]> getDescriptions() {
		return descriptions;
	}

	/**
	 * @param descriptions
	 *            the descriptions to set
	 */
	public void setDescriptions(List<String[]> descriptions) {
		this.descriptions = descriptions;
	}

	/**
	 * @return the sizes
	 */
	public List<int[]> getSizes() {
		return sizes;
	}

	/**
	 * @param sizes
	 *            the sizes to set
	 */
	public void setSizes(List<int[]> sizes) {
		this.sizes = sizes;
	}

	/**
	 * @return the total
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            the total to set
	 */
	public void setTotal(int total) {
		this.total = total;
	}

	/**
	 * @return the sections
	 */
	public int getSections() {
		return sections;
	}

	/**
	 * @param sections
	 *            the sections to set
	 */
	public void setSections(int sections) {
		this.sections = sections;
	}

	public String geTotalCaption(int row) {
		return totalCaptions[row];
	}

	public String getSectionCaption(int row) {
		return barCaptions[row];
	}

	public double getDistributionPixels(Element tableCell, int row, int index) {
		int totalSize = tableCell.getClientWidth() - 4;
		// int totalSize = tableCell.getOffsetWidth() - 2;
		final int value = sizes.get(row)[index];
		if (value == 0 && showEmpty) {
			if (minimumSectionWidth > (totalSize / sections))
				return totalSize / sections;
			else if (minimumSectionWidth > 0)
				return minimumSectionWidth;
			else
				return 0.0;
		} else if (value == 0 && !showEmpty)
			return 0.0;
		else if (total == 0 || minimumSectionWidth > (totalSize / sections))
			return totalSize / sections;
		else {
			if (minimumSectionWidth > 0) {
				totalSize -= minimumSectionWidth * sections;
				return minimumSectionWidth + totalSize / total * value;
			} else
				return totalSize / total * value;
		}
	}

	public String[] getColorsCSSNames(int sectionIndex) {
		if (colorMode == 1) {
			if (sectionIndex == 0)
				return new String[] { "#000", "#fff" };
			if (sectionIndex == sections - 1)
				return new String[] { "#fff", "#000" };
			int value = 256 / sections * (sectionIndex + 1);
			if (value > 200)
				return new String[] {
						"rgb(" + value + "," + value + "," + value + ")",
						"#000" };
			else
				return new String[] {
						"rgb(" + value + "," + value + "," + value + ")",
						"#fff" };
		} else if (colorMode == 2) {
			if (sectionIndex == 0)
				return new String[] { "#000", "#fff" };
			if (sectionIndex == sections - 1)
				return new String[] { "#ff0000", "#fff" };
			int value = 256 / sections * (sectionIndex + 1);
			return new String[] { "rgb(" + value + "," + 0 + "," + 0 + ")",
					"#fff" };
		}
		return new String[] { "#000", "#fff" };
	}

	public void sort() {

	}

}
